﻿using System;
using System.Collections.Generic;
using System.Text;
using OgilvyOne.UKNow.Model;
using OgilvyOne.UKNow.DALFactory;
using ECCIT;
using System.Linq;
using OgilvyOne.UKNow.IDAL;
using OgilvyOne.UKNow.DAL;

namespace OgilvyOne.UKNow.BLL
{
    public class WarmupsCommentBLL
    {
        public int LanguageID { get; set; }
        public WarmupsCommentBLL(int langID)
        {
            this.LanguageID = langID;
        }
        //根据活动的ID(评论表中的一部分评论的所有信息)获取该活动的所有的评论的所有信息
        public IList<WarmupCommentModel> SelectCommentByID(int Warmup_ID)
        {
            IDALWarmupComment use = WarmupCommentDALFactory.Create(LanguageID);
            return use.GetAllCommentsByWarmupID(Warmup_ID);
        }
        // 根据评论id获取i该评论
        public WarmupCommentModel getCommentByCid(int CommentId)
        {
            return WarmupCommentDALFactory.Create(LanguageID).GetWarmupCommentModelByID(CommentId);
        }

        public PagingList GetCommentIndexPage(int?wid,PagingParameter pagingParams)
        {
            IDALWarmupComment use = WarmupCommentDALFactory.Create(LanguageID);
            return use.GetPublishedCommentsByWarmupID(wid, pagingParams);
        }
        //修改单个作品的发布状态
        public void modPubOne(int wcid)
        {
            List<int?> list = new List<int?>();
            list.Add(wcid);

            IDALWarmupComment use = WarmupCommentDALFactory.Create(LanguageID);

            WarmupCommentModel model = use.GetWarmupCommentModelByID(wcid); //查看该评论的当前发布状态
            if (model.State > 0) //该评论的当前为发布状态
            {
                use.UnPublish(list);
                //设置为该评论的state为0 不发布 (传递参数 eid 和 int=0)
            }
            else
            {
                use.Publish(list);
                //设置该评论的state为1 发布     (传递参数 eid 和 int=1)
            }
        }
        //mff
        //删除单个评论
        public ResultInfo delWarmupComment(int wcid)
        {
            ResultInfo result = new ResultInfo(false);
            IList<int?> list = new List<int?>();
            list.Add(wcid);
            IDALWarmupComment use = WarmupCommentDALFactory.Create(LanguageID);

            /*
             * 代码修改
             * 修改人员：刘扬
             * 修改日期：2012-02-09
             * 修改原因：在删除作品评论后对作品的评论数量字段进行修改
             * */
            WarmupCommentModel commentModel = WarmupCommentDALFactory.Create(LanguageID).GetWarmupCommentModelByID(wcid);
            if (commentModel == null) return result;

            result = use.Delete(list);
            if (result.Result)
                result = WarmupDALFactory.Create(LanguageID).SubtractCommentCount(commentModel.WarmupID);

            return result;
        }
        //mff

        public ResultInfo Save(WarmupCommentModel model)
        {
            int? id = model.WarmupID;
            ResultInfo result = WarmupCommentDALFactory.Create(LanguageID).Save(model);
            if (result.Result && id.HasValue)   // 保存成功且不是新增评论数据
                WarmupDALFactory.Create(LanguageID).AddCommentCount(model.WarmupID);

            return result;
        }
    }
}